<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Weixin\Controller;

use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class ServiceController extends WeixinController
{

    public function youka()
    {


        $uid = $_COOKIE['user']['uid'];
        if (IS_POST) {

            $password = I('post.password');
            $number = I('post.number');
            $type = I('post.type');
            // $yh=D('Ptyh')->where(['uid'=>$uid])->find();
            $account = '15518311221';
            $orderid = date("YmdHis") . rand(100000, 999999);
            $price = '0.990';
            list($msec, $sec) = explode(' ', microtime());
            $timestamp = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
            $json = array(
                'account' => $account,
                'orderId' => $orderid,
                'cardNumber' => $number,
                'cardPwd' => $password,
                'face' => $type,
                'price' => $price,//与鼎信的折扣
                'effectTime' => date("Y-m-d", time() + 86400),
                'timestamp' => $timestamp,
            );
            $sign = '';
            foreach ($json as $key => $v) {
                $sign .= $key . $v;
            }
            $sign = $sign . 'eec62b20c54142188bfad1460ca6c691';
            $json['sign'] = md5($sign);
            $url = 'http://api.jiayouka.cn:11140/userApi/supply/gatewayV1';
            $res = $this->send_request($url, $json);

            // print_r($res);die;
            $price = '0.98';

            if ($res['code'] == '10010') {
                // 受理成功  存下订单
                $data = array(

                    'uid' => $uid,
                    'orderId' => $orderid,
                    'cardNumber' => $number,
                    'cardPwd' => $password,
                    'face' => $type,
                    'yh_money' => $type * $price,
                    'price' => $price,//与商户之间的折扣
                    'time' => time(),
                    'effectTime' => date("Y-m-d", time() + 86400),
                    'status' => 1,//已受理
                    're_status' => 0,//核销状态

                );
                $id = D('Youka')->add($data);
                echo $id;
                die;
                // 需要补充查询方法定时查询油卡是否已核销

            } else if ($res['code'] == '1005' || $res['code'] == '1009' || $res['code'] == '10017' || $res['code'] == '10021' || $res['code'] == '10022' || $res['code'] == '1005') {

                print_r($res['message']);
                die;

            } else {
                echo "提交失败";
                die;
            }
        } else {
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            C('WEB_SITE_TITLE', ' 油卡回收');
            $this->display();
        }

    }

    function youka_check()
    {
        $account = '15518311221';
        $list = D('Youka')->where(['re_status' => 0])->select();
        foreach ($list as $key => $v) {

            $json = array(
                'account' => $account,
                'orderId' => $v['orderId'],
            );
            $sign = '';
            foreach ($json as $key => $va) {
                $sign .= $key . $va;
            }
            $sign = $sign . 'eec62b20c54142188bfad1460ca6c691';
            $json['sign'] = md5($sign);
            $url = 'http://api.jiayouka.cn:11140/userApi/supply/queryOrder';
            $res = $this->send_request($url, $json);
            if ($res['code'] == '10010') {

                //1 核销成功  0  处理中 2 核销失败

                $id = D('Youka')->where(['orderId' => $res['orderId']])->save(['re_status' => $res['state']]);
                if ($id > 0) {
                    // 增加余额记录余额
                    $ga = D('Gather')->where(['uid' => $v['uid']])->find();
                    $t_money = $ga['money'] + $v['yh_money'];
                    D('Gather')->where(['uid' => $v['uid']])->save(['money' => $t_money]);
                    D('Youka')->where(['orderId' => $res['orderId']])->save(['t_money' => $t_money]);

                }


            }
        }
    }


    function send_request($request, $params, $method = 'get', $https = false)
    {
        // 以get方式提交
        if ($method == 'get') {
            $request = $request . $this->create_url($params);
        }

        // return $request;

        $this->ch = curl_init($request);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);// 设置不显示结果，储存入变量
        // curl_setopt($this->ch, CURLOPT_TIMEOUT, self::TIMEOUT); // 设置超时限制防止死循环

        // 判断是否以https方式访问
        if ($https) {
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        }

        if ($method == 'post') {        // 以post方式提交
            curl_setopt($this->ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
        }

        $tmpInfo = curl_exec($this->ch); // 执行操作
        if (curl_errno($this->ch)) {
            echo 'Errno:' . curl_error($this->ch);//捕抓异常
        }
        curl_close($this->ch); // 关闭CURL会话
        return json_decode($tmpInfo, true);
        return $tmpInfo; // 返回数据
    }

    function create_url($data)
    {
        $temp = '?';
        foreach ($data as $key => $item) {
            $temp = $temp . $key . '=' . $item . '&';
        }
        return substr($temp, 0, -1);
    }


}
